/*
 * @Author: lei
 * @Description: priority_queue使用示例
 * @FilePath: /code/priority_queue.cc
 */
#include <queue>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

//g++ -o priority_queue priority_queue.cc -std=c++17
template<typename T> 
void print_queue(T& q) {
    while(!q.empty()) {
        std::cout << q.top() << " ";
        q.pop();
    }
    std::cout << std::endl;
}

class Data
{
public:
	Data(int a,double b):int_(a),double_(b){}
	int int_;
	double double_;
};
std::ostream& operator<<(std::ostream &out,Data& d)
{
	out << d.int_ << " " << d.double_ << std::endl;
	return out;
}

int main() {
    std::priority_queue<int> q;//默认大顶堆，内存中的序列是9,8,7,6,5,4,3,2,1,0
 
    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q.push(n);
    print_queue(q);

    std::priority_queue<int, std::vector<int>, std::greater<int> > q2;//小顶堆,内存中的序列是0,1,2,3,4,5,6,7,8,9
    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q2.push(n);
    print_queue(q2);

	Data d1(3,2.43);
	Data d2(3,1.43);
    auto cmp = [](const Data& d1,const Data& d2){return d1.int_==d2.int_?d1.double_>d2.double_:d1.int_>d2.int_;};//自定义算子
    std::priority_queue<Data, std::vector<Data>, decltype(cmp)> q3(cmp);
    q3.push(d1);
	q3.push(d2);
	Data dd =q3.top();
	std::cout<<dd;

	return 0;
}
/*输出：
9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 
3 1.43
*/